package com.example.cyh.bczapp.Code.fragment;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.cyh.bczapp.Code.BLL.WordBLL;
import com.example.cyh.bczapp.R;

/**
 * Created by cyh on 2017/5/8.
 */

public class TranslateFragment extends Fragment {
    private View AllView;
    private String Englishword;
    private String result = "";
    private Handler hd = new Handler();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        AllView = inflater.inflate(R.layout.fragment_findword, container, false);
        return AllView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        ((EditText) AllView.findViewById(R.id.Eword)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Englishword = s.toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        WordBLL wb = new WordBLL();
                        result = wb.Translation(((EditText) AllView.findViewById(R.id.Eword)).getText().toString());
                        hd.post(new Runnable() {
                            @Override
                            public void run() {
                                ((EditText) AllView.findViewById(R.id.Cword)).setText(result);
                                result = "";
                            }
                        });
                    }
                }).start();
            }
        });
    }
}
